local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local CodeAnimationTool=_G.CommonOHOH.load.CodeAnimationTool();
local EffectId={
    base_meat="Effect.MeatPerfab",
    base_wins="Effect.winsPerfab",
    base_diamond="Effect.diamondPerfab",
}
local AddNumberId="Effect.AddNumber"
local ItemTool=_G.CommonOHOH.load.ItemTool()
local gBattleUiCtrl=nil
local TweenService = game:GetService("TweenService")
local TrianDamgeId="Effect.TrianDamge"
local BigNumberHelper = _G.ClientOHOH.load.BigNumberHelper();


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    
    self.player=_G.client.local_player
    self.playerCtrl= self.player:GetComponent(_G.CommonOHOH.load.Common_PlayerToolCtrl())

    gBattleUiCtrl=self.player:GetPrivateComponent("BattleHomeGuiCtrl")

    
    --掉落肉
     self.meatCount=self.parent.cfg.meat
     self.drop=self.parent.cfg.drop

    self.SizeId=self.parent:GetSizeId()

    self.parent.HurtEvent:Connect(function(damge,attacker)
        
        self:FlyDamge(damge,"Effect.HP_Hurt_"..self.SizeId)
        self:PlayEffect("Effect.SkibiHurt_"..self.SizeId,self.parent:GetPosition())

    end)

    self.parent:AttributeValueChanged("DeadTime",function()

        self:PlayEffect("Effect.SkibiDie_"..self.SizeId,self.parent:GetPosition())
        local meat= self.playerCtrl:GetDrop({base_meat=self.meatCount})
        for k,v in pairs(meat) do
            self:DropEffect(k,v)
        end
        if  self.drop then
            local data=self.playerCtrl:GetDrop(self.drop)
            for k,v in pairs(data) do
                self:DropEffect(k,v)
            end
        end

    end)

end;

--计算特效数量
function M.GetEffectObj(self,item_id,count)
    if count>8 then
       return 8
    end
    return  count
end


-- 播放掉落特效
function M.DropEffect(self,key,count)
    local boomPos=self.parent:GetPosition()
    local posList={}
    local sum=self:GetEffectObj(key,count)
    local effectId=EffectId[key]
    CodeAnimationTool.Drop3DAnimation.Play(function()
        return _G.POOL:GetObj(effectId)
    end,function(e)
        table.insert(posList,e.Position)
        _G.POOL:PushObj(effectId,e)
        sum=sum-1
        if sum==0 then
            gBattleUiCtrl:AddBaseItem(key,posList,count)
        end
    end,sum,boomPos)
end

function M.PlayEffect(self,id,pos)
    local e=_G.POOL:GetObj(id)
    require(e.PlayCtrl).Play(pos,function()
        _G.POOL:PushObj(id,e)
    end)
end

    -- 创建Tween信息
local tweenInfo = TweenInfo.new(
    1,                -- 时间
    Enum.EasingStyle.Linear    -- 线性过渡
)
function M.FlyDamge(self,damge,id)
    local position=self.parent:GetPosition()
    local effect=_G.POOL:GetObj(id)
    effect.Attachment.BillboardGui.TextLabel.Text="-"..BigNumberHelper:ToString(damge)
    effect.Position=position
    local endPos=position+Vector3.new(0,10,math.random(-5, 5))
    local tween = TweenService:Create(effect, tweenInfo, {Position=endPos})
    tween.Completed:Connect(function()
        tween:Destroy()
        _G.POOL:PushObj(id,effect)
    end)
    tween:Play()
end



function M.dtor(self)
	M.super.dtor(self);
end



return M